<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>哈三天眼监控</title>
  <script src="https://cdn.jsdelivr.net/npm/flv.js/dist/flv.min.js"></script>
  <style>
    /* 弹出窗口样式 */
    .modal {
      display: none;
      position: fixed;
      z-index: 1000;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      background-color: rgba(0,0,0,0.7);
    }

    .modal-content {
      background-color: #fefefe;
      margin: 5% auto;
      padding: 20px;
      border: 1px solid #888;
      width: 80%;
      max-width: 800px;
      border-radius: 8px;
      position: relative;
    }

    .close {
      color: #aaa;
      float: right;
      font-size: 28px;
      font-weight: bold;
      cursor: pointer;
      position: absolute;
      right: 20px;
      top: 10px;
    }

    .close:hover,
    .close:focus {
      color: black;
      text-decoration: none;
    }

    .open-btn {
      background-color: #4CAF50;
      border: none;
      color: white;
      padding: 15px 32px;
      text-align: center;
      text-decoration: none;
      display: inline-block;
      font-size: 16px;
      margin: 4px 2px;
      cursor: pointer;
      border-radius: 4px;
    }

    .open-btn:hover {
      background-color: #45a049;
    }

    .video-container {
      text-align: center;
      margin-top: 20px;
    }

    h1 {
      text-align: center;
      color: #333;
      margin-bottom: 20px;
    }
  </style>
</head>
<body>
  <!-- 打开按钮 -->
  <div style="text-align: center; margin-top: 50px;">
    <button class="open-btn" onclick="openModal()">打开监控窗口</button>
  </div>

  <!-- 弹出窗口 -->
  <div id="myModal" class="modal">
    <div class="modal-content">
      <span class="close" onclick="closeModal()">&times;</span>
      <h1>哈三-场地南侧</h1>
      <div class="video-container">
        <video id="videoElement" controls width="640" height="360"></video>
      </div>
    </div>
  </div>

  <script>
    let flvPlayer = null;

    // 打开弹出窗口
    function openModal() {
      document.getElementById('myModal').style.display = 'block';
      // 延迟一点时间再初始化播放器，确保DOM元素已经显示
      setTimeout(initPlayer, 100);
    }

    // 关闭弹出窗口
    function closeModal() {
      document.getElementById('myModal').style.display = 'none';
      // 停止播放器
      if (flvPlayer) {
        flvPlayer.pause();
        flvPlayer.unload();
        flvPlayer.detachMediaElement();
        flvPlayer.destroy();
        flvPlayer = null;
      }
    }

    // 初始化播放器
    function initPlayer() {
      // 检查浏览器是否支持 MSE (Media Source Extensions)
      if (flvjs.isSupported()) {
        // 初始化 FLV.js 播放器
        const videoElement = document.getElementById('videoElement');
        flvPlayer = flvjs.createPlayer({
          type: 'flv',
          url: 'ws://localhost:8866/live?url=rtsp://0vpreview:e018d418@59.80.55.116:10554/1064:12:0:0.sdp',
          //url: 'wss://tums.chdgc.com.cn/vms/jessica/hdhshd/1068.flv',
        });
        flvPlayer.attachMediaElement(videoElement);
        flvPlayer.load();
        flvPlayer.play();
      } else {
        alert('FLV.js 不支持您的浏览器');
      }
    }

    // 点击窗口外部关闭弹出窗口
    window.onclick = function(event) {
      const modal = document.getElementById('myModal');
      if (event.target == modal) {
        closeModal();
      }
    }

    // ESC键关闭弹出窗口
    document.addEventListener('keydown', function(event) {
      if (event.key === 'Escape') {
        closeModal();
      }
    });
  </script>
</body>
</html> 